// pages/my/sign/index.js
const app = getApp()
var zhenzisms = require('../../../utils/zhenzisms.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    password: '',
    password2: '',
    code: '',
    err: '',
    status:'',
    statusdata:'',
    pswerr: '',
    statuserr:'',
    inviteCode:[]
  },
  getPhoneValue (e) {
    this.setData({
      phone: e.detail
    })
  },
  // getStatusValue(e) {
  //   this.setData({
  //     status: e.detail
  //   })
  // },
  // checkStatus(e) {
  //   if(this.data.status != ''){
  //     //修改1234
  //     if (this.data.inviteCode.indexOf(this.data.status) == -1) {
  //       this.setData({
  //         statuserr: '请输入正确的驗證碼或不填',
  //         status:''
  //       })
  //     }else {
  //       this.setData({
  //         statuserr: '',
  //          statusdata: '1'
  //       })
  //     }
  //   }
  //   else {
  //     this.setData({
  //       statuserr: '',
  //       statusdata:'2'
  //     })
  //   }
  // },
  checkPhone () {
    if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
      this.setData({
        err: '请输入正确的手机号码'
      })
    }else{
      this.setData({
        err: ''
      })
    } 
  },
  // checkCode () {
  //   var number = this.data.phone
  //   zhenzisms.client.sendCode(function (res) {
  //     console.log(res.data);
  //   }, '13229852657', '验证码为:{code}', 321, 60, 6);
  // },
  getPasswordValue (e) {
    this.setData({
      password: e.detail
    })
  },
  getPassword2Value(e) {
    this.setData({
      password2: e.detail
    })
  },
  checkPassword () {
    if(this.data.password != this.data.password2){
      this.setData({
        pswerr: '两次输入密码不一致'
      })
    }else{
      this.setData({
        pswerr: ''
      })
    }
  },
  sign () {
 if (!this.data.phone || !this.data.password || !this.data.password2 || this.data.statuserr){
      wx.showToast({
        title: '请完成信息',
        images: '../../../static/icons/err.png',
        duration: 2000,
      })
      return
    }
    var that = this
    wx.login({
      success(res) {
        var model = {
          account_id: that.data.phone,
          password: that.data.password,
          code: res.code,
          msg: wx.getStorageSync('userInfo')
        }
        app.api.sign(model, (res) => {
          if (res.statusCode == 401) {
            wx.showToast({
              title: res.data,
              images: '../../../static/icons/err.png',
              duration: 2000
            })
          }else{
            wx.showModal({
              title: '注册成功',
              content: '前往登录界面?',
              success(res) {
                if (res.confirm) {
                  wx.navigateBack({
                    
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
          
        })
      }
    })  
  },
  getAllInviteCode:function(){
      var that = this
      var temp_key=[]
      app.api.getData('getAllInvites', 'none', (res) => {
          res.data.forEach(item=>{
              item.inviteKey.forEach(keys=>{
                  temp_key.push(keys)
              })
          })
          that.setData({
              inviteCode:temp_key
          })
          console.log(that.data.inviteCode)
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var apiUrl = 'https://sms_developer.zhenzikj.com'
    // var appId = '102396'
    // var appSecret = '3b6d0e59-d248-4770-b839-ffe2f11dd4ef'
    // zhenzisms.client.init(apiUrl, appId, appSecret);
    if (!wx.getStorageSync('userInfo')) {
      this.setData({
        show: true
      })
    }
    var that = this
    that.getAllInviteCode()
  },
  getUserInfo(userInfo) {
    if (!userInfo.detail.rawData || !userInfo.detail.signature) {
      wx.showModal({
        title: '提示',
        content: '你没有授权,不能享受菜多多的贴♥服务哦'
      })
      return
    }
    wx.setStorageSync('userInfo', userInfo.detail.rawData)
    this.setData({
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})